import java.util.*;


public class Solution {
    /**
     * 最大乘积
     * @param A int整型一维数组 
     * @return long长整型
     */
    public long solve (int[] a) {
        if(a.length <= 4){
            a = bobblesort(a);
            long max1 = (long)a[0] *(long)a[1] * (long)a[2];
            long max2 = (long)a[a.length - 1] * (long)a[a.length - 2] * (long)a[0];
            return Math.max(max1,max2);
        }else{
            // write code here
            int[] x = findfivenum(a);
            long max1 = (long)x[0] *(long)x[1] * (long)x[2];
            long max2 = (long)x[x.length - 1] * (long)x[x.length - 2] * (long)x[0];
            return Math.max(max1,max2);
            }
    }
    public static int[] findfivenum(int[] a) {
        int[] c = {a[0],a[1],a[2]};
        c = bobblesort(c);
        int max = c[0];
        int semax = c[1];
        int thrmax = c[2];
        int zxnum = 0;
        int senum = 0;
        if(a[0] < a[1]){
            zxnum = a[0];
            senum = a[1];
        }else{
            zxnum = a[1];
            senum = a[0];
        }
        for (int i = 1; i < a.length; i++) {
//          找前三个最大的数
            if(a[i] > max){
                thrmax = semax;
                semax = max;
                max = a[i];
//            }else if (a[i] < max && a[i] > semax){
            }else if (a[i] > semax){
                thrmax = semax;
                semax = a[i];
//            }else if (a[i] < semax && a[i] > thrmax){
            }else if (a[i] > thrmax){
                thrmax = a[i];
            }
//          找出最小的和第二小的
            if (a[i] < zxnum) {
                senum = zxnum;
                zxnum = a[i];
            } else if (a[i] < senum) {
                senum = a[i];
            }
        }
        int[] b = {max,semax,thrmax,senum,zxnum};
        return b;
    }
    public static int[] bobblesort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - i - 1; j++) {
                if (a[j] < a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }

        }
        return a;
    }
}